package cpms.admin.controller.tintuc;

import cpms.data.dao.TinTucDAO;
import cpms.data.entity.NhanVien;
import cpms.data.entity.TinTuc;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.imageio.ImageIO;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

@ManagedBean
@ViewScoped
public class TinTucBean {
    
    // save and update
    private int mstt;
    private String tieude;
    private String mota;
    private String hinhdaidien;
    private String noidung;
    private NhanVien nhanVienDangTin;
    private NhanVien nhanVienDuyetTin;
    private int trangthai;
    
    // upload
    private StreamedContent uploadedImage;
    
    // dao
    private TinTucDAO dao;
    
    @PostConstruct
    public void init() {
        dao = new TinTucDAO();
    }
    
    public void handleFileUpload(FileUploadEvent event) {
        UploadedFile uploadedFile = event.getFile();
        try {
            // copy to temp folder
            File tempDir = new File(System.getProperty("java.io.tmpdir"));
            String path = tempDir.getPath() + File.separator + uploadedFile.getFileName();
            InputStream in = uploadedFile.getInputstream();
            OutputStream out = new FileOutputStream(new File(path));
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            in.close();
            out.flush();
            out.close();
            
//            //Graphic Text
//            BufferedImage bufferedImg = new BufferedImage(100, 25, BufferedImage.TYPE_INT_RGB);
//            Graphics2D g2 = bufferedImg.createGraphics();
//            g2.drawString("This is a text", 0, 10);
//            ByteArrayOutputStream os = new ByteArrayOutputStream();
//            ImageIO.write(bufferedImg, "png", os);
//            uploadedImage = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png"); 
            
//            uploadedImage = new DefaultStreamedContent(new FileInputStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void pageNew() {
        
    }
    
    public void save() {
        TinTuc tt = new TinTuc();
        System.out.println("save...");
    }
    
    public void update() {
        
    }

    public int getMstt() {
        return mstt;
    }

    public void setMstt(int mstt) {
        this.mstt = mstt;
    }

    public String getHinhdaidien() {
        return hinhdaidien;
    }

    public void setHinhdaidien(String hinhdaidien) {
        this.hinhdaidien = hinhdaidien;
    }

    public String getMota() {
        return mota;
    }

    public void setMota(String mota) {
        this.mota = mota;
    }

    public NhanVien getNhanVienDangTin() {
        return nhanVienDangTin;
    }

    public void setNhanVienDangTin(NhanVien nhanVienDangTin) {
        this.nhanVienDangTin = nhanVienDangTin;
    }

    public NhanVien getNhanVienDuyetTin() {
        return nhanVienDuyetTin;
    }

    public void setNhanVienDuyetTin(NhanVien nhanVienDuyetTin) {
        this.nhanVienDuyetTin = nhanVienDuyetTin;
    }

    public String getNoidung() {
        return noidung;
    }

    public void setNoidung(String noidung) {
        this.noidung = noidung;
    }

    public String getTieude() {
        return tieude;
    }

    public void setTieude(String tieude) {
        this.tieude = tieude;
    }

    public int getTrangthai() {
        return trangthai;
    }

    public void setTrangthai(int trangthai) {
        this.trangthai = trangthai;
    }

    public StreamedContent getUploadedImage() {
        return uploadedImage;
    }
    
}